﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SpLocalizer.Common.Extensions
{
    public static class ControlExtentions
    {
        public static void RunInvoke(this Control control, Action action)
        {
            if (control.InvokeRequired)
                control.Invoke(action);
            else
                action();
        }

        public static void RunBeginInvoke(this Control control, Action action)
        {
            if (control.InvokeRequired)
                control.BeginInvoke(action);
            else
                action();
        }

        public static void RunBeginInvoke<T>(this Control control, Action<T> action, T args)
        {
            if (control.InvokeRequired)
                control.BeginInvoke(action, args);
            else
                action(args);
        }

        public static void AddItems(this ComboBox control, IEnumerable<string> items)
        {
            if (items.IsNull())
                throw new ArgumentNullException("items", "items is null");
            items.Iter(item => control.RunInvoke(() => control.Items.Add(item)));
        }
    }
}